package com.novel.controller.system;

import com.novel.common.exception.CustomerException;
import com.novel.controller.other.BaseController;
import com.novel.pojo.system.ChapterOrder;
import com.novel.pojo.system.User;
import com.novel.service.ChapterOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("chapter/order")
public class ChapterOrderController extends BaseController {

    @Autowired
    private ChapterOrderService chapterOrderService;

    /**
     * 章节购买
     */
    @PostMapping("buy/{chapterId}")
    public boolean buyChapter(@PathVariable("chapterId") String chapterId) {

        User user = getUserInformation();
        if (user == null) {
            throw new CustomerException("用户未登录!");
        }

        ChapterOrder chapterOrder = new ChapterOrder();
        chapterOrder.setId(UUID.randomUUID().toString());
        chapterOrder.setChapterId(chapterId);
        chapterOrder.setUserId(user.getUserId());
        chapterOrder.setCreatedTime(new Date());
        chapterOrder.setCreatedIp(getUserIpAddress());
        chapterOrder.setCost(3);

        return chapterOrderService.insertChapterOrderRecord(chapterOrder);
    }

}